{* $Id$ *}
{include_core file="admin/lib/header.html" title="分类展示"}
<div class="page-header border-bottom clearfix">
  <h2 class="left">分类管理<span>{if $info}编辑{else}添加{/if}分类</span></h2>
  <a href="{AnUrl('category/admin')}" class="btn btn-default btn-sm right">&lt;&nbsp;返回分类列表</a>
</div>
<!--start:分类新增-->
<div class="add-form">
  <form class="js_creatForm" action="{AnUrl('category/admin/save')}" method="post" enctype="multipart/form-data">
    <input type="hidden" class="js_currId" name="id" value="{$category_info.id}">
    <input type="hidden" name="info[res_name]" value="{$res_name}">
    <table class="table" cellpadding="0" cellspacing="0">
      {*
      <tr>
        <th><label for="" class="add-f-label">分类类型：</label></th>
        <td>
          <select class="add-f-slt1 js_type" name="info[res_name]">
            <option value="goods" {if $category_info.res_name == 'goods'}selected{/if}>商品分类</option>
            <option value="article" {if $category_info.res_name == 'article'}selected{/if}>资讯分类</option>
          </select>
          <div class="tips"><span style="line-height: 34px;color: #f00;margin-right: 10px;">*</span>请选择分类类型</div>
        </td>
      </tr>
      *}
      <tr>
        <th><label for="" class="add-f-label">所属分类：</label></th>
        <td>
          <div class="js_parent">
            {$parent_path = explode('-',$category_info.path)}
            {$count = count($parent_path)}
            {foreach $parent_path as $k=>$v}
            {*if $count == 1 || $k < ($count-1)*}
              <select class="add-f-slt1 js_select js_sel_0" >
                  {if $k == 0}
                  <option value="0">顶级分类</option>
                  {else}
                  <option value="{$parent_path.$k}">请选择</option>
                  {/if}
                {$category_parent_list = category_helper::get_category_list_by_parent_id($v,$category_info.res_name)}
                {foreach $category_parent_list as $key=>$value}
                  {if $id != {$value.id}}
                  <option value="{$value.id}" {if $value.id == $parent_path[$k+1]}selected{/if}>{$value.title}</option>
                  {/if}
                  }
                {/foreach}
              </select>
            {*/if*}
            {/foreach}
          </div>
          <input type="hidden" class="parent_id js_parent_id" name="info[parent_id]" value="{if $category_info.parent_id}{$category_info.parent_id}{else}0{/if}" empty-data="{if $category_info}1{/if}" path-data="{$category_info.path}">
          <div class="tips">
            <span style="line-height: 34px;color: #f00;margin-right: 10px;">*</span>
            请选择父级分类
          </div>
        </td>
      </tr>
      <tr>
        <th><label for="" class="add-f-label">分类标题：</label></th>
        <td>
          <input name="info[title]" style="width:490px;" value="{$category_info.title}" type="text" class="add-f-ipt1 js_title">
          <div class="tips"><span style="line-height: 34px;color: #f00;margin-right: 10px;">*</span>分类标题会显示在相应内容的标题位置，最多输入 <span style="color:red;">20</span> 字，还可输入 <span class="js_titleNum" style="color:red;">20</span> 字</div>
        </td>
      </tr>
      <tr><th>&nbsp;</th><td class="noborder"><button type="submit" class="btn btn-success js_submit">&nbsp;&nbsp;&nbsp;确认&nbsp;&nbsp;&nbsp;</button></td></tr>
    </table>
  </form>
</div>
<!--end:分类新增-->
<script>
var resName = 'category';
function setNum(thisClass, maxNum) {
  var presentNum = $('.'+thisClass).val().length;
  var remainNum  = maxNum - presentNum;
  if (remainNum < 0) {
    $('.'+thisClass).val($('.'+thisClass).val().substr(0,maxNum));
    $('.'+thisClass+'Num').text(0);
  } else {
    $('.'+thisClass+'Num').text(remainNum);
  }
}

// ajax获取分类列表
var currId   = $('.js_currId').val();
var res_name = 'goods';
var parent_id = 0;
var selNum = 0;
function getCategoryList(res_name, parent_id, firstSel){
  $.getJSON("{AnUrl('category/admin/check_menu')}", { res_name:res_name, parent_id:parent_id }, function(data){
    if (firstSel) {
      selNum = 0;
      $('.js_sel_'+selNum).nextAll().remove();
      $('.js_sel_'+selNum).html('<option value="0">顶级分类</option>');
    } else if (data.info == 'ok' && data.category_list != '') {
        selNum++;
        $('.js_parent').append('<select class="add-f-slt1 js_select js_sel_'+selNum+'"></select>');
        $('.js_sel_'+selNum).append('<option value="'+parent_id+'">请选择..</option>');
    }
    if (data.info == 'ok' && data.category_list != '') {
      var optionCount = data.category_list.length;
      for (var i = 0; i < optionCount; i++) {
        if (currId != data['category_list'][i].id) {
          $('.js_sel_'+selNum).append('<option value="'+data['category_list'][i].id+'">'+data['category_list'][i].title+'</option>');
        }
      };
    };
  });
}

$(function(){
  //初始化分类列表
  var res_name = "{$res_name}";
  if ($('.js_parent_id').attr('empty-data') != 1) {
    getCategoryList(res_name, 0, 1);
  }


  //选择分类类型
  // $('.js_type').change(function(){
  //   res_name = $(this).val();
  //   getCategoryList(res_name, 0, 1);
  // });

  //选择分类调取子分类
  $('.js_parent').on('change', '.js_select', function(){
    var currParent = $(this).val();
    var firstValue = $(this).children().eq(0).attr('value');
    $('.js_parent_id').val(currParent);
    $(this).nextAll().remove();
    if (firstValue != currParent) {
      getCategoryList(res_name, currParent, 0);
    };
  })

  // 初始化剩余字数
  setNum('js_title', 20);
  $('.js_title').keyup(function(){
    setNum('js_title', 20);
  });

  // 提交验证
  $('.js_submit').click(function(e){
    e.preventDefault();
    var title      = $.trim($('.js_title').val());
    if (!title) {
      showMsg('标题不能为空');
      return;
    }

    $('.js_creatForm').submit();
  });
});
</script>
{include_core file="admin/lib/footer.html"}